#include <iostream>
#include <string.h>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;

template <typename T = int, int Capacity = 10>
class Stack {
public:
    Stack()
        :m_data(new T[Capacity]())
        , m_size(0)
        , m_top(0)
        , m_capacity(Capacity)
    {
    }
    //判空
    bool is_empty() {
        return m_size == 0;
    }
    //判满
    bool is_pull() {
        return m_size == Capacity;
    }
    //入栈
    void push(T element) {
        if (m_size == Capacity) {
            cout << "stack is pull" << endl;
        }
        m_data[m_top] = element;
        m_top++;
        m_size++;
    }
    //弹栈
    void pop() {
        if (m_size == 0) {
            cout << "stack is empty" << endl;
            return;
        }
        m_size--;
        m_top--;
    }
    //查看栈顶
    void peek() {
        if (m_size == 0) {
            cout << "stack is empty" << endl;
            return;
        }
        cout << m_data[m_top - 1] << endl;
    }
    ~Stack()
    {
        if (m_data) {
            delete[] m_data;
            m_data = nullptr;
        }
    }
    //打印
    void print() {
        cout << "size = " << m_size << endl
            << "capacity = " << Capacity << endl
            << "top = " << m_top << endl
            << "data : " << endl;
        for (int i = 0; i < m_top; i++) {
            cout << m_data[i] << " ";
        }
        cout << endl;
    }
private:
    T* m_data;
    int m_size;
    int m_top;
    int m_capacity;
};

//对char*类型的入栈特化
template<>
void Stack<const char*>::push(const char* pstr)
{
    cout << "特化入栈" << endl;
    if (m_size == m_capacity) {
        cout << "stack is pull" << endl;
        return;
    }
    char* new_data = new char[strlen(pstr) + 1] {};
    strcpy(new_data, pstr);
    m_data[m_top] = new_data;
    m_size++;
    m_top++;
}
//对char*类型的弹栈特化
template<>
void Stack<const char*>::pop() {
    cout << "特化弹栈" << endl;
    if (m_size == 0) {
        cout << "stack is empty" << endl;
        return;
    }
    m_size--;
    m_top--;
    delete[] m_data[m_top];
}
//对char*类型的析构特化
template<>
Stack<const char*>::~Stack() {
    cout << "特化析构" << endl;

    if (m_data) {
        for (int i = 0; i < m_size; i++) {
            delete[] m_data[i];
        }
        delete[] m_data;
        m_data = nullptr;
    }
}

void test() {
    Stack<> s1;
    s1.print();
    cout << s1.is_empty() << endl;
    cout << s1.is_pull() << endl;
    s1.peek();
    s1.pop();
    s1.push(1);
    s1.push(3);
    s1.push(6);
    s1.push(2);
    cout << "----------" << endl;
    s1.print();
    s1.pop();
    cout << "----------" << endl;
    s1.print();

}
void test2() {
    Stack<string, 5> s1;
    s1.print();
    cout << s1.is_empty() << endl;
    cout << s1.is_pull() << endl;
    s1.peek();
    s1.pop();
    s1.push("123");
    s1.push("hello");
    s1.push("lilei");
    s1.push("999");
    cout << "----------" << endl;
    s1.print();
    s1.pop();
    cout << "----------" << endl;
    s1.print();
    s1.peek();

}
void test3() {
    Stack<const char*> s1;
    s1.print();
    cout << s1.is_empty() << endl;
    cout << s1.is_pull() << endl;
    s1.peek();
    s1.pop();
    s1.push("123");
    s1.push("hello");
    s1.push("lilei");
    s1.push("999");
    cout << "----------" << endl;
    s1.print();
    s1.pop();
    cout << "----------" << endl;
    s1.print();
    s1.peek();

}
int main() {
    // test();
    // test2();
    test3();
    return 0;
}